package org.example.nebula.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileNameUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.example.nebula.entity.input.EdgeDataDirInput;
import org.example.nebula.entity.input.NgInsertEdgeInput;
import org.example.nebula.entity.input.NgInsertTagInput;
import org.example.nebula.entity.input.TagDataDirInput;
import org.example.nebula.entity.out.Result;
import org.example.nebula.service.ImportDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;

/**
 * @author: jszhang
 * @date: 2024/10/25-11:51
 */
@Api(tags = "导入图谱数据")
@Slf4j
@RestController
@RequestMapping(value = "/data/import")
public class ImportDataController {

    @Autowired
    private ImportDataService importDataService;

    @ApiOperation("向图空间插入节点信息")
    @PostMapping(value = "/insertTagData")
    public Result<?> insertTagData(@RequestBody NgInsertTagInput ngInsertTagInput) {

        long startTime = System.currentTimeMillis();
        importDataService.insertTagData(ngInsertTagInput);
        log.info("{}共计耗时{}",ngInsertTagInput.getTagName(), System.currentTimeMillis() - startTime);

        return Result.OK();
    }

    @ApiOperation("批量-向图空间插入节点信息")
    @PostMapping(value = "batch/insertTagData")
    public Result<?> insertTagDirData(@RequestBody TagDataDirInput input) {
        String dirPath = input.getDirPath();
        File[] files = FileUtil.ls(dirPath);
        for (File file : files) {
            NgInsertTagInput ngInsertTagInput = new NgInsertTagInput();
            ngInsertTagInput.setTagName(input.getTagName());
            ngInsertTagInput.setCsvPath(file.getAbsolutePath());
            ngInsertTagInput.setSpaceName(input.getSpaceName());
            long startTime = System.currentTimeMillis();
            importDataService.insertTagData(ngInsertTagInput);
            log.info("{}共计耗时{}", FileNameUtil.mainName(file), System.currentTimeMillis() - startTime);
        }

        return Result.OK();
    }

    @ApiOperation("向图空间插入边信息")
    @PostMapping(value = "/insertEdgeData")
    public Result<?> insertEdgeData(@RequestBody NgInsertEdgeInput ngInsertEdgeInput) {

        long startTime = System.currentTimeMillis();
//        dataService.insertEdgeData(ngInsertEdgeInput);
        importDataService.insertEdgeDataWithNoRank(ngInsertEdgeInput);
        log.info("{}共计耗时{}", ngInsertEdgeInput.getEdgeName(), System.currentTimeMillis() - startTime);
        return Result.OK();
    }

    @ApiOperation("批量-向图空间插入边信息")
    @PostMapping(value = "batch/insertEdgeDirData")
    public Result<?> insertEdgeDirData(@RequestBody EdgeDataDirInput input) {
        String dirPath = input.getDirPath();
        File[] files = FileUtil.ls(dirPath);
        for (File file : files) {
            NgInsertEdgeInput ngInsertEdgeInput = new NgInsertEdgeInput();
            ngInsertEdgeInput.setSpaceName(input.getSpaceName());
            ngInsertEdgeInput.setCsvPath(file.getAbsolutePath());
            ngInsertEdgeInput.setEdgeName(input.getEdgeName());
            long startTime = System.currentTimeMillis();
//        dataService.insertEdgeData(ngInsertEdgeInput);
            importDataService.insertEdgeDataWithNoRank(ngInsertEdgeInput);
            log.info("{}共计耗时{}",  input.getDirPath(), System.currentTimeMillis() - startTime);
        }

        return Result.OK();
    }

}
